namespace SmartSoft.Controls
{
    using SmartSoft;
    using SmartSoft.Properties;
    using SmartSoft.ADP;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class FormOpenningEditor : FormControl
    {
        private ToolStripButton btnAdd;
        private ToolStripButton btnClose;
        private ToolStripButton btnSave;
        private IContainer components;
        private bool ib_ExchangeType;
        private bool ib_NewChecked;
        private decimal ii_ExchangeRateOfBook;
        private string is_AddTitleText;
        private string is_EditTitleText;
        private object is_SavedMasterOrder;
        private string is_TranCurrencyCode;
        private FormOpenningLister oLocalFormLister;
        private DataRow oLocalRow;
        private DataTable oLocalTable;
        private object oUserDefined1;
        private object oUserDefined2;
        private object oUserDefined3;
        private ToolStripSeparator tbrSep01;
        private ToolStripSeparator tbrSep02;
        private ToolStrip Toolbars;

        public FormOpenningEditor()
        {
            this.is_SavedMasterOrder = "";
            this.is_TranCurrencyCode = AppInstance.DefaultCurrency;
            this.ii_ExchangeRateOfBook = 1M;
            this.ib_ExchangeType = true;
            this.InitializeComponent();
        }

        public FormOpenningEditor(object poUserDefined1, object poUserDefined2, object poUserDefined3)
        {
            this.is_SavedMasterOrder = "";
            this.is_TranCurrencyCode = AppInstance.DefaultCurrency;
            this.ii_ExchangeRateOfBook = 1M;
            this.ib_ExchangeType = true;
            this.InitializeComponent();
            this.UserDefined1 = poUserDefined1;
            this.UserDefined2 = poUserDefined2;
            this.UserDefined3 = poUserDefined3;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.OnSave())
            {
                this.NewChecked = true;
                this.OnReset();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.OnSave())
            {
                base.Close();
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private string GetTitleMessage()
        {
            if (!this.NewChecked)
            {
                return this.EditTitleText;
            }
            return this.AddTitleText;
        }

        private void InitializeComponent()
        {
            this.Toolbars = new System.Windows.Forms.ToolStrip();
            this.btnAdd = new System.Windows.Forms.ToolStripButton();
            this.tbrSep01 = new System.Windows.Forms.ToolStripSeparator();
            this.btnSave = new System.Windows.Forms.ToolStripButton();
            this.tbrSep02 = new System.Windows.Forms.ToolStripSeparator();
            this.btnClose = new System.Windows.Forms.ToolStripButton();
            this.Toolbars.SuspendLayout();
            this.SuspendLayout();
            // 
            // Toolbars
            // 
            this.Toolbars.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.btnAdd,
            this.tbrSep01,
            this.btnSave,
            this.tbrSep02,
            this.btnClose});
            this.Toolbars.Location = new System.Drawing.Point(0, 0);
            this.Toolbars.Name = "Toolbars";
            this.Toolbars.Size = new System.Drawing.Size(521, 25);
            this.Toolbars.TabIndex = 0;
            this.Toolbars.Text = "Toolbars";
            // 
            // btnAdd
            // 
            this.btnAdd.Image = global::SmartSoft.Properties.Resources.BALANCE1;
            this.btnAdd.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new System.Drawing.Size(58, 22);
            this.btnAdd.Text = "Thêm";
            this.btnAdd.ToolTipText = "Thêm mới (F2)";
            this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
            // 
            // tbrSep01
            // 
            this.tbrSep01.Name = "tbrSep01";
            this.tbrSep01.Size = new System.Drawing.Size(6, 25);
            // 
            // btnSave
            // 
            this.btnSave.Image = global::SmartSoft.Properties.Resources.SAVE1;
            this.btnSave.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(47, 22);
            this.btnSave.Text = "Lưu";
            this.btnSave.ToolTipText = "Ghi lại và Thoát (Ctrl+Enter)";
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // tbrSep02
            // 
            this.tbrSep02.Name = "tbrSep02";
            this.tbrSep02.Size = new System.Drawing.Size(6, 25);
            // 
            // btnClose
            // 
            this.btnClose.Image = global::SmartSoft.Properties.Resources.CLOSE;
            this.btnClose.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new System.Drawing.Size(58, 22);
            this.btnClose.Text = "Thoát";
            this.btnClose.ToolTipText = "Thoát (Esc)";
            this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
            // 
            // FormOpenningEditor
            // 
            this.ClientSize = new System.Drawing.Size(521, 183);
            this.Controls.Add(this.Toolbars);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "FormOpenningEditor";
            this.Toolbars.ResumeLayout(false);
            this.Toolbars.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        protected virtual void OnAdd()
        {
            this.NewChecked = true;
        }

        protected virtual void OnReset()
        {
        }

        public virtual bool OnSave()
        {
            if (this.LocalTable.Columns.Contains("Stt"))
            {
                this.LocalFormLister.OnSetPosition("Stt", this.SavedMasterOrder);
            }
            else
            {
                this.LocalFormLister.OnSetPosition("Stt0", this.SavedMasterOrder);
            }
            return true;
        }

        public virtual void OnShow()
        {
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            Keys keys = keyData;
            switch (keys)
            {
                case Keys.Escape:
                    base.Close();
                    return true;

                case Keys.F2:
                    if (this.btnAdd.Enabled && this.OnSave())
                    {
                        this.OnAdd();
                        this.OnReset();
                    }
                    return true;
            }
            if (keys != (Keys.Control | Keys.Return))
            {
                return base.ProcessCmdKey(ref msg, keyData);
            }
            if (this.OnSave())
            {
                base.Close();
            }
            return true;
        }

        public bool ShowMessage(DataRow row, int pi_Result)
        {
            switch (pi_Result)
            {
                case -3:
                    row.RejectChanges();
                    MessageBox.Show("Mã lồng nhau!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    return true;

                case -1:
                    row.RejectChanges();
                    MessageBox.Show("Mã đã tồn tại!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    return true;
            }
            return false;
        }

        [Description("The title text when adding."), Category("Editor properties"), DefaultValue("")]
        public string AddTitleText
        {
            get
            {
                return this.is_AddTitleText;
            }
            set
            {
                this.is_AddTitleText = value;
                this.Text = value;
            }
        }

        [Category("Editor properties"), DefaultValue(""), Description("The title text when editing.")]
        public string EditTitleText
        {
            get
            {
                return this.is_EditTitleText;
            }
            set
            {
                this.is_EditTitleText = value;
            }
        }

        [Description("The exchange rate of the current currency. This to calculate the invoice amount."), Browsable(false), Category("Editor properties"), DefaultValue(0)]
        public decimal ExchangeRate
        {
            get
            {
                if (this.ExchangeType)
                {
                    return this.ExchangeRateOfBook;
                }
                return (1M / this.ExchangeRateOfBook);
            }
        }

        [Browsable(false), Category("Editor properties"), Description("The exchange rate of the current currency. This to write to the invoice."), DefaultValue(1)]
        public decimal ExchangeRateOfBook
        {
            get
            {
                return this.ii_ExchangeRateOfBook;
            }
            set
            {
                this.ii_ExchangeRateOfBook = value;
            }
        }

        [Category("Editor properties"), DefaultValue(0), Description("The exchange type of the current currency. This to write to the invoice."), Browsable(false)]
        public bool ExchangeType
        {
            get
            {
                return this.ib_ExchangeType;
            }
            set
            {
                this.ib_ExchangeType = value;
            }
        }

        [Browsable(false)]
        public DataRow LocalCurrentRow
        {
            get
            {
                return this.oLocalRow;
            }
            set
            {
                this.oLocalRow = value;
                if ((this.oLocalRow != null) && (this.oLocalRow.RowState == DataRowState.Unchanged))
                {
                    this.OnShow();
                }
                this.OnReset();
            }
        }

        [Browsable(false)]
        public FormOpenningLister LocalFormLister
        {
            get
            {
                return this.oLocalFormLister;
            }
            set
            {
                this.oLocalFormLister = value;
            }
        }

        [Browsable(false)]
        public DataTable LocalTable
        {
            get
            {
                return this.oLocalTable;
            }
            set
            {
                this.oLocalTable = value;
            }
        }

        [Browsable(false)]
        public bool NewChecked
        {
            get
            {
                return this.ib_NewChecked;
            }
            set
            {
                this.ib_NewChecked = value;
                this.Text = this.GetTitleMessage();
            }
        }

        public object SavedMasterOrder
        {
            get
            {
                return this.is_SavedMasterOrder;
            }
            set
            {
                if (this.is_SavedMasterOrder != value)
                {
                    this.is_SavedMasterOrder = value;
                }
            }
        }

        [Description("The transaction currency code of current invoice."), Category("Editor properties"), DefaultValue(""), Browsable(false)]
        public string TranCurrencyCode
        {
            get
            {
                return this.is_TranCurrencyCode;
            }
            set
            {
                if (this.is_TranCurrencyCode != value)
                {
                    this.is_TranCurrencyCode = value;
                    if (string.IsNullOrEmpty(this.TranCurrencyCode) || (AppInstance.DefaultCurrency == this.is_TranCurrencyCode))
                    {
                        this.ExchangeRateOfBook = 1M;
                        this.ExchangeType = true;
                    }
                    else
                    {
                        SSInvoiceLibDataAccess access = new SSInvoiceLibDataAccess();
                        bool flag = true;
                        decimal num = access.ExchangeRate(this.TranCurrencyCode, ref flag);
                        this.ExchangeType = flag;
                        this.ExchangeRateOfBook = num;
                    }
                }
            }
        }

        public object UserDefined1
        {
            get
            {
                return this.oUserDefined1;
            }
            set
            {
                this.oUserDefined1 = value;
            }
        }

        public object UserDefined2
        {
            get
            {
                return this.oUserDefined2;
            }
            set
            {
                this.oUserDefined2 = value;
            }
        }

        public object UserDefined3
        {
            get
            {
                return this.oUserDefined3;
            }
            set
            {
                this.oUserDefined3 = value;
            }
        }
    }
}

